//
//  BaseViewController.m
//  dyss_v2
//
//  Created by LXF on 16/1/26.
//  Copyright © 2016年 First Facility. All rights reserved.
//

#import "BaseViewController.h"
#import "UIViewController+xx_skin.h"
#import <Masonry.h>
#import "Skin.h"
#import <CustomLogTools.h>

//#import "IQKeyboardManager_PACK.h"

@implementation BaseViewController

-(UINavigationController *)weakNavigationController{
    if (_weakNavigationController == nil) {
        _weakNavigationController = self.navigationController;
    }
    return _weakNavigationController;
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

#pragma mark - initial 1.0
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self prepareBackground];
    
    [self setupRightBarButton];
    
    [self willInitial];
    [self prepareInitial];
    [self didInitial];
}

-(void)prepareInitial{
    [self initData];
    
    [self initUI];
    [self initUI:self.referView];
    
    [self layoutContentView];
    [self layoutContentView:self.referView];
    
    [self bindEvent];
}

-(void)willInitial{};
-(void)initData{};
-(void)initUI{};
-(void)bindEvent{};
-(void)layoutContentView{};
-(void)didInitial{};

#pragma mark - initial 2.0

-(void)initUI:(UIView *)referView{}
-(void)layoutContentView:(UIView *)referView{};

-(UIView *)referView{
    return self.view;
}
-(void)setReferView:(UIView *)referView{
    self.view = referView;
}

#pragma mark - prepareBackgroundImageView
-(UIColor *)backgroundColor{
    return [UIColor skin:@"background_color"];
}

-(BOOL)needBackgroundImageView{
    return NO;
}
- (void)prepareBackground{
    if ([self needBackgroundImageView] && self.backgroundImageView) {
        [self.view insertSubview:self.backgroundImageView atIndex:0];
        self.view.backgroundColor = [UIColor whiteColor];
        [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }else{
        self.view.backgroundColor = self.backgroundColor;
    }
}
-(UIImageView *)backgroundImageView{
    if (_backgroundImageView == nil && self.backgroundImage != nil) {
        UIImageView *a = [[UIImageView alloc] initWithImage:self.backgroundImage];
        _backgroundImageView = a;
    }
    return _backgroundImageView;
}
-(UIImage *)backgroundImage{
    if (_backgroundImage == nil) {
        UIImage *a = [UIImage skin:@"tabbar_vc_backImage"];
        _backgroundImage = a;
    }
    return _backgroundImage;
}

#pragma mark - 是否需要键盘弹出监听
//- (BOOL)needKeyboardManager{
//    return NO;
//}

#pragma mark - 是否需要交互式弹出手势(重写)
/// 默认需要
- (BOOL)needInteractivePopGesture{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (self.needInteractivePopGesture == NO) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        SEL sel = @selector(setEnabledInteractivePopGesture:);
        if ([self.navigationController respondsToSelector:sel]){
            self.weakNavigationController = self.navigationController;
            [self.weakNavigationController setValue:@NO forKey:@"enabledInteractivePopGesture"];
        }
 #pragma clang diagnostic pop
    }
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if (self.needInteractivePopGesture == NO) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        SEL sel = @selector(setEnabledInteractivePopGesture:);
        if ([self.weakNavigationController respondsToSelector:sel]){
            [self.weakNavigationController setValue:@YES forKey:@"enabledInteractivePopGesture"];
        }
#pragma clang diagnostic pop
    }
}

#pragma mark - 是否需要导航栏右边的摁钮
- (void)setupRightBarButton{
    UIImage *image = [self needEnabledNavigationRightBarButton];
    if (image) {
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIBarButtonItem *rItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButton_onClick)];
        self.navigationItem.rightBarButtonItem = rItem;
    }
}
- (UIButton *)needEnabledNavigationRightBarButton{
    return nil;
}

-(void)rightBarButton_onClick{
    printW(@"请重写此方法...");
}

@end
